#include "interface.h"

GtkWidget*
create_launch (void)
{
  GtkWidget *launch;
  GtkWidget *vbox2;
  GtkWidget *vbox3;
  GtkWidget *frameServerSettings;
  GtkWidget *tableServerSettings;
  GtkWidget *entryDomain;
  GtkWidget *labelBase;
  GtkWidget *labelDomain;
  GtkWidget *labelPrefix;
  GtkWidget *entryBaseURL;
  GtkWidget *entryPrefix;
  GtkWidget *labelServer;
  GtkWidget *frameDaemonSettings;
  GtkWidget *tableDaemonSettings;
  GtkWidget *labelLogFile;
  GtkWidget *fileentryLogFile;
  GtkWidget *entryLogFile;
  GtkWidget *labelPort;
  GtkWidget *entryPort;
  GtkWidget *labelDaemonSettings;
  GtkWidget *hbuttonbox2;
  GtkWidget *buttonCancel;
//  GtkWidget *buttonHelp;
  GtkWidget *buttonLaunch;
  GtkWidget *alignment6;
  GtkWidget *hbox17;
  GtkWidget *image13;
  GtkWidget *label36;
  GtkTooltips *tooltips;

  gchar *value;
  GConfEngine *gc_engine = gconf_engine_get_default(); 
  
  tooltips = gtk_tooltips_new ();

  launch = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (launch), "w3live - Launch Daemon");
  gtk_window_set_wmclass (GTK_WINDOW (launch), "w3live-launch", "w3live-launch");
  gtk_window_set_resizable (GTK_WINDOW (launch), FALSE);
  gtk_window_set_destroy_with_parent (GTK_WINDOW (launch), TRUE);

  vbox2 = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox2);
  gtk_container_add (GTK_CONTAINER (launch), vbox2);

  vbox3 = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox3);
  gtk_box_pack_start (GTK_BOX (vbox2), vbox3, FALSE, FALSE, 0);

  frameServerSettings = gtk_frame_new (NULL);
  gtk_widget_show (frameServerSettings);
  gtk_box_pack_start (GTK_BOX (vbox3), frameServerSettings, FALSE, FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (frameServerSettings), 5);

  tableServerSettings = gtk_table_new (3, 2, FALSE);
  gtk_widget_show (tableServerSettings);
  gtk_container_add (GTK_CONTAINER (frameServerSettings), tableServerSettings);
  gtk_container_set_border_width (GTK_CONTAINER (tableServerSettings), 5);
  gtk_table_set_row_spacings (GTK_TABLE (tableServerSettings), 5);
  gtk_table_set_col_spacings (GTK_TABLE (tableServerSettings), 24);

  entryDomain = gtk_entry_new ();
  gtk_widget_show (entryDomain);
  gtk_table_attach (GTK_TABLE (tableServerSettings), entryDomain, 1, 2, 1, 2,
                    (GtkAttachOptions) (GTK_SHRINK),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_tooltips_set_tip (tooltips, entryDomain, "Domain of webser being watched (i.e. .cs.rpi.edu)", NULL);
  gtk_entry_set_width_chars (GTK_ENTRY (entryDomain), 35);

  labelBase = gtk_label_new ("Base URL:");
  gtk_widget_show (labelBase);
  gtk_table_attach (GTK_TABLE (tableServerSettings), labelBase, 0, 1, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_label_set_justify (GTK_LABEL (labelBase), GTK_JUSTIFY_LEFT);
  gtk_misc_set_alignment (GTK_MISC (labelBase), 0, 0.5);

  labelDomain = gtk_label_new ("Domain:");
  gtk_widget_show (labelDomain);
  gtk_table_attach (GTK_TABLE (tableServerSettings), labelDomain, 0, 1, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_label_set_justify (GTK_LABEL (labelDomain), GTK_JUSTIFY_LEFT);
  gtk_misc_set_alignment (GTK_MISC (labelDomain), 0, 0.5);

  labelPrefix = gtk_label_new ("Path Prefix:");
  gtk_widget_show (labelPrefix);
  gtk_table_attach (GTK_TABLE (tableServerSettings), labelPrefix, 0, 1, 2, 3,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_label_set_justify (GTK_LABEL (labelPrefix), GTK_JUSTIFY_LEFT);
  gtk_misc_set_alignment (GTK_MISC (labelPrefix), 0, 0.5);

  entryBaseURL = gtk_entry_new ();
  gtk_widget_show (entryBaseURL);
  gtk_table_attach (GTK_TABLE (tableServerSettings), entryBaseURL, 1, 2, 0, 1,
                    (GtkAttachOptions) (GTK_SHRINK),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_tooltips_set_tip (tooltips, entryBaseURL, "Base URL of webserver being watched (i.e. http://www.cs.rpi.edu)", NULL);
  gtk_entry_set_text (GTK_ENTRY (entryBaseURL), "http://");
  gtk_entry_set_width_chars (GTK_ENTRY (entryBaseURL), 35);

  entryPrefix = gtk_entry_new ();
  gtk_widget_show (entryPrefix);
  gtk_table_attach (GTK_TABLE (tableServerSettings), entryPrefix, 1, 2, 2, 3,
                    (GtkAttachOptions) (GTK_SHRINK),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_tooltips_set_tip (tooltips, entryPrefix, "Prefix of server requests to index (i.e. http://www.cs.rpi.edu/~moorthy)", NULL);
  gtk_entry_set_width_chars (GTK_ENTRY (entryPrefix), 35);

  labelServer = gtk_label_new ("Server Settings");
  gtk_widget_show (labelServer);
  gtk_frame_set_label_widget (GTK_FRAME (frameServerSettings), labelServer);
  gtk_label_set_justify (GTK_LABEL (labelServer), GTK_JUSTIFY_LEFT);

  frameDaemonSettings = gtk_frame_new (NULL);
  gtk_widget_show (frameDaemonSettings);
  gtk_box_pack_start (GTK_BOX (vbox3), frameDaemonSettings, FALSE, FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (frameDaemonSettings), 5);

  tableDaemonSettings = gtk_table_new (2, 2, FALSE);
  gtk_widget_show (tableDaemonSettings);
  gtk_container_add (GTK_CONTAINER (frameDaemonSettings), tableDaemonSettings);
  gtk_container_set_border_width (GTK_CONTAINER (tableDaemonSettings), 10);
  gtk_table_set_row_spacings (GTK_TABLE (tableDaemonSettings), 5);
  gtk_table_set_col_spacings (GTK_TABLE (tableDaemonSettings), 42);

  labelLogFile = gtk_label_new ("Log File:");
  gtk_widget_show (labelLogFile);
  gtk_table_attach (GTK_TABLE (tableDaemonSettings), labelLogFile, 0, 1, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_label_set_justify (GTK_LABEL (labelLogFile), GTK_JUSTIFY_LEFT);
  gtk_misc_set_alignment (GTK_MISC (labelLogFile), 0, 0.5);

  fileentryLogFile = gnome_file_entry_new ("logfilehist", "Web Server Extended Log File");
  gtk_widget_show (fileentryLogFile);
  gtk_table_attach (GTK_TABLE (tableDaemonSettings), fileentryLogFile, 1, 2, 0, 1,
                    (GtkAttachOptions) (GTK_SHRINK),
                    (GtkAttachOptions) (0), 0, 0);
  gnome_file_entry_set_modal (GNOME_FILE_ENTRY (fileentryLogFile), TRUE);

  entryLogFile = gnome_file_entry_gtk_entry (GNOME_FILE_ENTRY (fileentryLogFile));
  gtk_widget_show (entryLogFile);
  gtk_tooltips_set_tip (tooltips, entryLogFile, "Path to web server's extended log file (i.e. /usr/local/apache2/logs/extended_log)",
NULL);

  labelPort = gtk_label_new ("Port:");
  gtk_widget_show (labelPort);
  gtk_table_attach (GTK_TABLE (tableDaemonSettings), labelPort, 0, 1, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_label_set_justify (GTK_LABEL (labelPort), GTK_JUSTIFY_LEFT);
  gtk_misc_set_alignment (GTK_MISC (labelPort), 0, 0.5);

  entryPort = gtk_entry_new ();
  gtk_widget_show (entryPort);
  gtk_table_attach (GTK_TABLE (tableDaemonSettings), entryPort, 1, 2, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_tooltips_set_tip (tooltips, entryPort, "Port to launch w3live daemon on (default 1030)", NULL);
  gtk_entry_set_text (GTK_ENTRY (entryPort), "1030");
  gtk_entry_set_width_chars (GTK_ENTRY (entryPort), 6);

  labelDaemonSettings = gtk_label_new ("Daemon Settings");
  gtk_widget_show (labelDaemonSettings);
  gtk_frame_set_label_widget (GTK_FRAME (frameDaemonSettings), labelDaemonSettings);
  gtk_label_set_justify (GTK_LABEL (labelDaemonSettings), GTK_JUSTIFY_LEFT);

  hbuttonbox2 = gtk_hbutton_box_new ();
  gtk_widget_show (hbuttonbox2);
  gtk_box_pack_end (GTK_BOX (vbox2), hbuttonbox2, FALSE, FALSE, 0);
  gtk_container_set_border_width (GTK_CONTAINER (hbuttonbox2), 10);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox2), GTK_BUTTONBOX_END);
  gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbuttonbox2), 5);

  buttonCancel = gtk_button_new_from_stock ("gtk-cancel");
  gtk_widget_show (buttonCancel);
  gtk_container_add (GTK_CONTAINER (hbuttonbox2), buttonCancel);
  GTK_WIDGET_SET_FLAGS (buttonCancel, GTK_CAN_DEFAULT);
/*
  buttonHelp = gtk_button_new_from_stock ("gtk-help");
  gtk_widget_show (buttonHelp);
  gtk_container_add (GTK_CONTAINER (hbuttonbox2), buttonHelp);
  GTK_WIDGET_SET_FLAGS (buttonHelp, GTK_CAN_DEFAULT);
*/
  buttonLaunch = gtk_button_new ();
  gtk_widget_show (buttonLaunch);
  gtk_container_add (GTK_CONTAINER (hbuttonbox2), buttonLaunch);
  GTK_WIDGET_SET_FLAGS (buttonLaunch, GTK_CAN_DEFAULT);

  alignment6 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment6);
  gtk_container_add (GTK_CONTAINER (buttonLaunch), alignment6);

  hbox17 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox17);
  gtk_container_add (GTK_CONTAINER (alignment6), hbox17);

  image13 = gtk_image_new_from_stock ("gtk-execute", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image13);
  gtk_box_pack_start (GTK_BOX (hbox17), image13, FALSE, FALSE, 0);

  label36 = gtk_label_new_with_mnemonic ("_Launch");
  gtk_widget_show (label36);
  gtk_box_pack_start (GTK_BOX (hbox17), label36, FALSE, FALSE, 0);
  gtk_label_set_justify (GTK_LABEL (label36), GTK_JUSTIFY_LEFT);

  gtk_signal_connect (GTK_OBJECT (launch), "destroy",
                      GTK_SIGNAL_FUNC (gtk_widget_destroy),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (buttonCancel), "clicked",
                             GTK_SIGNAL_FUNC (gtk_widget_destroy),
                             GTK_OBJECT (launch));
/*
  gtk_signal_connect_object (GTK_OBJECT (buttonHelp), "clicked",
                             GTK_SIGNAL_FUNC (daemonLaunchHelp),
                             GTK_OBJECT (NULL));
*/
  gtk_signal_connect        (GTK_OBJECT (buttonLaunch), "clicked",
                             GTK_SIGNAL_FUNC (daemonRun),
                             GTK_OBJECT (launch));

  if((value = gconf_engine_get_string(gc_engine,"/apps/w3live/domain",NULL))) {
     gtk_entry_set_text(GTK_ENTRY(entryDomain),value);
     g_free(value);
  }
  gtk_entry_set_width_chars (GTK_ENTRY (entryDomain), 35);

  if((value = gconf_engine_get_string(gc_engine,"/apps/w3live/base",NULL))) {
    gtk_entry_set_text(GTK_ENTRY(entryBaseURL),value);
    g_free(value);
  }
  else
	  gtk_entry_set_text (GTK_ENTRY (entryBaseURL), "http://");

  if((value = gconf_engine_get_string(gc_engine,"/apps/w3live/prefix",NULL))) {
     gtk_entry_set_text(GTK_ENTRY(entryPrefix),value);
     g_free(value);
  }
  gtk_entry_set_width_chars (GTK_ENTRY (entryPrefix), 35);

  if((value = gconf_engine_get_string(gc_engine,"/apps/w3live/logfile",NULL))) {
    gtk_entry_set_text(GTK_ENTRY(entryLogFile),value);
    g_free(value);
  }
  else
    gtk_entry_set_text(GTK_ENTRY(entryLogFile),"/usr/local/apache/logs/extended_log");

  if((value = gconf_engine_get_string(gc_engine,"/apps/w3live/port",NULL))) {
     gtk_entry_set_text(GTK_ENTRY(entryPort),value);
     g_free(value);
  }
  else
     gtk_entry_set_text (GTK_ENTRY (entryPort), "1030");

  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (launch, launch, "launch");
  GLADE_HOOKUP_OBJECT (launch, vbox2, "vbox2");
  GLADE_HOOKUP_OBJECT (launch, vbox3, "vbox3");
  GLADE_HOOKUP_OBJECT (launch, frameServerSettings, "frameServerSettings");
  GLADE_HOOKUP_OBJECT (launch, tableServerSettings, "tableServerSettings");
  GLADE_HOOKUP_OBJECT (launch, entryDomain, "entryDomain");
  GLADE_HOOKUP_OBJECT (launch, labelBase, "labelBase");
  GLADE_HOOKUP_OBJECT (launch, labelDomain, "labelDomain");
  GLADE_HOOKUP_OBJECT (launch, labelPrefix, "labelPrefix");
  GLADE_HOOKUP_OBJECT (launch, entryBaseURL, "entryBaseURL");
  GLADE_HOOKUP_OBJECT (launch, entryPrefix, "entryPrefix");
  GLADE_HOOKUP_OBJECT (launch, labelServer, "labelServer");
  GLADE_HOOKUP_OBJECT (launch, frameDaemonSettings, "frameDaemonSettings");
  GLADE_HOOKUP_OBJECT (launch, tableDaemonSettings, "tableDaemonSettings");
  GLADE_HOOKUP_OBJECT (launch, labelLogFile, "labelLogFile");
  GLADE_HOOKUP_OBJECT (launch, fileentryLogFile, "fileentryLogFile");
  GLADE_HOOKUP_OBJECT (launch, entryLogFile, "entryLogFile");
  GLADE_HOOKUP_OBJECT (launch, labelPort, "labelPort");
  GLADE_HOOKUP_OBJECT (launch, entryPort, "entryPort");
  GLADE_HOOKUP_OBJECT (launch, labelDaemonSettings, "labelDaemonSettings");
  GLADE_HOOKUP_OBJECT (launch, hbuttonbox2, "hbuttonbox2");
  GLADE_HOOKUP_OBJECT (launch, buttonCancel, "buttonCancel");
  //GLADE_HOOKUP_OBJECT (launch, buttonHelp, "buttonHelp");
  GLADE_HOOKUP_OBJECT (launch, buttonLaunch, "buttonLaunch");
  GLADE_HOOKUP_OBJECT (launch, alignment6, "alignment6");
  GLADE_HOOKUP_OBJECT (launch, hbox17, "hbox17");
  GLADE_HOOKUP_OBJECT (launch, image13, "image13");
  GLADE_HOOKUP_OBJECT (launch, label36, "label36");
  GLADE_HOOKUP_OBJECT_NO_REF (launch, tooltips, "tooltips");

  gconf_engine_unref(gc_engine);
  
  return launch;
}

void
daemonRun                             (GtkButton        *button,
                                       gpointer          user_data)
{
	GtkWidget *window = (GtkWidget *)user_data;
	GtkWidget *windowDaemon = NULL;
	char *base, *domain, *prefix, *log, *port;
	
	GConfEngine *gc_engine = NULL;

	base   = (char *)gtk_entry_get_text(GTK_ENTRY(lookup_widget(window,"entryBaseURL")));
	domain = (char *)gtk_entry_get_text(GTK_ENTRY(lookup_widget(window,"entryDomain")));
	prefix = (char *)gtk_entry_get_text(GTK_ENTRY(lookup_widget(window,"entryPrefix")));
	log    = (char *)gtk_entry_get_text(GTK_ENTRY(lookup_widget(window,"entryLogFile")));
	port   = (char *)gtk_entry_get_text(GTK_ENTRY(lookup_widget(window,"entryPort")));

	gc_engine = gconf_engine_get_default();
	gconf_engine_set_string(gc_engine,"/apps/w3live/domain",domain,NULL);
	gconf_engine_set_string(gc_engine,"/apps/w3live/prefix",prefix,NULL);
	gconf_engine_set_string(gc_engine,"/apps/w3live/base",base,NULL);
	gconf_engine_set_string(gc_engine,"/apps/w3live/port",port,NULL);
	gconf_engine_set_string(gc_engine,"/apps/w3live/logfile",log,NULL);
	gconf_engine_unref(gc_engine);
	
	windowDaemon = create_daemon(base,prefix,domain,port,log);

	gtk_widget_destroy(window);
	gtk_widget_show(windowDaemon);
}

void
daemonLaunchHelp                       (GtkButton       *button,
                                        gpointer         user_data)
{

}
